// comps/authSetting/authSetting.js
const md5 = require('../../utils/md5.js')
const common = require('../../utils/util.js')
const app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isUserInfo: Boolean,
        isLogin: Boolean
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        getUserInfo: function(e) {
            const that = this
            if (e.detail.errMsg == 'getUserInfo:ok') {
                wx.showLoading({
                    title: '登录中...',
                })
                let data = {}
                let key = wx.getStorageSync('token')
                let sign = 'cds'
                let timestamp = Date.parse(new Date())
                data.key = key
                data.timestamp = timestamp
                data = that.objKeySort(data)
                for (let i in data) {
                    sign += (i + data[i])
                }
                sign += 'cds'
                sign = md5(sign)
                data.sign = sign
                data.code = wx.getStorageSync('code')
                data.phone = wx.getStorageSync('userPhone')
                data.wx_sm = 1
                data.encryptedData = e.detail.encryptedData
                data.iv = e.detail.iv
                wx.request({
                    url: 'https://www.meiboshi.vip/api/Login/phone_unionid',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    method: 'POST',
                    data: data,
                    success: function(res) {
                        wx.login({
                            success(loginRes) {
                                wx.setStorageSync('code', loginRes.code)
                            }
                        })
                        const data = res.data.datas
                        if (res.data.code == 204) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '网络繁忙，请重新登录',
                                showCancel: false,
                                success(res) {}
                            })
                        } else {
                            wx.hideLoading()
                            wx.setStorageSync('isUserInfo', true)
                            wx.setStorageSync('unionID', 1)
                            common.refresh()
                        }
                    }
                })
            } else {
                wx.setStorageSync('isUserInfo', true)
                common.refresh()
            }
        },
        getPhoneNumber(e) {
            if (e.detail.errMsg == 'getPhoneNumber:ok') {
                wx.showLoading({
                    title: '登录中...',
                })
                let loginData
                if (wx.getStorageSync('userShare')) {
                    loginData = {
                        encryptedData: e.detail.encryptedData,
                        iv: e.detail.iv,
                        code: wx.getStorageSync('code'),
                        belong_refer: wx.getStorageSync('userShare')
                    }
                } else {
                    loginData = {
                        encryptedData: e.detail.encryptedData,
                        iv: e.detail.iv,
                        code: wx.getStorageSync('code')
                    }
                }
                wx.request({
                    url: 'https://www.meiboshi.vip/api/Login/phone_bing',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    method: 'POST',
                    data: loginData,
                    success: function(res) {
                        wx.login({
                            success(loginRes) {
                                wx.setStorageSync('code', loginRes.code)
                            }
                        })
                        const data = res.data.datas
                        if (res.data.code == 204) {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '网络繁忙，请重新登录',
                                showCancel: false,
                                success(res) {

                                }
                            })
                        } else {
                            wx.hideLoading()
                            wx.setStorageSync('loginStatus', false)
                            wx.setStorageSync('isLogin', true)
                            wx.setStorageSync('token', data.token)
                            wx.setStorageSync("roleType", data.role_type)
                            wx.setStorageSync("storeType", data.store_type)
                            wx.setStorageSync("storeId", data.store_id)
                            wx.setStorageSync("userId", data.user_id)
                            wx.setStorageSync("userPhone", data.phone)
                            if (data.is_unoid == 0) {
                                wx.setStorageSync('isUserInfo', false)
                                wx.setStorageSync('unionID', 0)
                            } else {
                                wx.setStorageSync('isUserInfo', true)
                                wx.setStorageSync('unionID', 1)
                            }
                            common.refresh()
                        }
                    }
                })
            }
        },
        myCatchTouch: function() {
            return
        },
        objKeySort: function(obj) {
            let newkey = Object.keys(obj).sort()
            let newObj = {}
            for (let i = 0; i < newkey.length; i++) {
                newObj[newkey[i]] = obj[newkey[i]]
            }
            return newObj
        }
    }
})